package com.daihou.BCJ.ruletree;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.daihou.config.TheAuth;
import com.daihou.model.ceres.RuleTreeUrlEnum;
import com.daihou.utils.DHConfigFile;
import com.daihou.utils.DHHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.*;

public class RuleTreeSave {
    private static final Logger logger = LoggerFactory.getLogger(RuleTreeSave.class);
    private String url;
    Map<String, String> headers = new HashMap<>();
    private int ruleTreeId;

    @BeforeTest(description = "测试准备工作,获取Authorization对象")
    public void beforeTest() throws IOException {
        url = DHConfigFile.getUrl("DaiHou_URL","ruletree", RuleTreeUrlEnum.RULETREESAVE.toString());
        //设置请求头中的Authorization
        headers.put("Authorization", TheAuth.getAuth());
        headers.put("X-UserName", "admin");
    }

    @Test(description = "规则树添加")
    public void ruleTreeAddTest() {
        String ruleTreeName = "测试规则树test" + UUID.randomUUID().toString();
        String body = "{\"ruleTreeId\":\"\",\"ruleTreeName\":" + "\"" + ruleTreeName + "\"" + ",\"ruleTreeContent\":[{\"id\":1,\"pid\":null,\"type\":\"START\"},{\"pid\":1,\"type\":\"CASE_SCREEN\",\"ruleConditions\":[{\"leftExpression\":{\"name\":\"productType\",\"displayName\":\"产品类型\",\"javaType\":\"string\"},\"operator\":\"CONTAINS\",\"rightExpression\":{\"value\":[\"CREDIT\"],\"displayName\":\"信用标\"}}],\"id\":2},{\"pid\":2,\"type\":\"INTERGROUP_RATE\",\"ruleConditions\":[{\"value\":\"DEFAULT\",\"displayName\":\"默认\"}],\"id\":3},{\"pid\":3,\"type\":\"COLLORG_RATE\",\"ruleConditions\":[{\"collOrg\":29,\"displayName\":\"好分期高额S1西安D4\",\"rate\":\"1\"}],\"id\":4}],\"creator\":\"admin\"}";
        JSONObject js = JSON.parseObject(body);

        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("规则树添加结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
        ruleTreeId = JSON.parseObject(result).getJSONObject("data").getIntValue("ruleTreeId");
    }

    @Test(description = "规则树编辑", dependsOnMethods = "ruleTreeAddTest")
    public void ruleTreeEditTest() {
        url = DHConfigFile.getUrl("DaiHou_URL","ruletree", RuleTreeUrlEnum.RULETREEEDIT.toString());
        String ruleTreeName = "测试规则树test" + UUID.randomUUID().toString();
        String body = "{\"ruleTreeId\":\"" + ruleTreeId + "\",\"ruleTreeName\":" + "\"" + ruleTreeName + "\"" + ",\"ruleTreeContent\":[{\"id\":1,\"pid\":null,\"type\":\"START\"},{\"pid\":1,\"type\":\"CASE_SCREEN\",\"ruleConditions\":[{\"leftExpression\":{\"name\":\"productType\",\"displayName\":\"产品类型\",\"javaType\":\"string\"},\"operator\":\"CONTAINS\",\"rightExpression\":{\"value\":[\"CREDIT\"],\"displayName\":\"信用标\"}}],\"id\":2},{\"pid\":2,\"type\":\"INTERGROUP_RATE\",\"ruleConditions\":[{\"value\":\"DEFAULT\",\"displayName\":\"默认\"}],\"id\":3},{\"pid\":3,\"type\":\"COLLORG_RATE\",\"ruleConditions\":[{\"collOrg\":29,\"displayName\":\"好分期高额S1西安D4\",\"rate\":\"1\"}],\"id\":4}],\"creator\":\"admin\"}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("规则树添加结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "规则树删除", dependsOnMethods = "ruleTreeEditTest")
    public void ruleTreeDeleteTest() {
        url = DHConfigFile.getUrl("DaiHou_URL","ruletree", RuleTreeUrlEnum.RULETREEDELETE.toString());
        JSONObject body = new JSONObject();
        body.put("ruleTreeId", ruleTreeId);
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("规则树删除结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }
}